package com.beverages.messages.controller;

import com.beverages.messages.client.MerchantHolderClient;
import com.beverages.messages.client.MerchantOrdersClient;
import com.beverages.messages.common.Result;
import com.beverages.messages.entity.dto.HoldersDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/merchant/messages/facility")
public class FacilityController {

    private final RestTemplate restTemplate;
    private static final String PYTHON_URL = "http://127.0.0.1:9000/send_message/";

    @Autowired
    public FacilityController(@Qualifier("restTemplateForPython") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    private MerchantHolderClient merchantHolderClient;

    @Autowired
    private MerchantOrdersClient merchantOrdersClient;

    @PostMapping("/receive")
    public String receiveMessage(@RequestBody String message) {
        // 处理接收到的消息
        log.info("Received from Python: " + message);
        return "Message received: " + message;
    }

    @PostMapping("/send")
    public String sendMessage(@RequestBody String message) {
        // 发送消息到Python端
        String response = restTemplate.getForObject(PYTHON_URL + message, String.class);
        return "Sent message to Python: " + message + "\nResponse: " + response;
    }

    @PostMapping("/send/holder")
    public Result sendPreparedOrdersToFacility(@RequestBody List<HoldersDTO> holderDTOs) {
        // 发送holderDTOs到Python端
        ResponseEntity<String> response = restTemplate.exchange(
                PYTHON_URL,  // 目标 URL，指向 Python 服务端
                HttpMethod.POST,  // 使用 POST 请求
                new HttpEntity<>(holderDTOs),  // 将 List 作为请求体发送
                String.class  // 期望响应的类型是 String
        );
        return Result.success();
    }

    @PostMapping("/receive/processing")
    public Result receiveProcessingOrder(@RequestBody Integer processingHolder) {
        log.info("Received processingOrderId from Python: " + processingHolder);
        merchantHolderClient.setOneProcessingBeverage(processingHolder);
        return Result.success();
    }

    @PostMapping("/receive/processed")
    public Result receiveProcessedOrder(@RequestBody Integer fullHolderId) {
        log.info("Received fullHolderId from Python: " + fullHolderId);
        merchantHolderClient.updateFullHolder(fullHolderId);
        return Result.success();
    }

    @PostMapping("/receive/pick-up")
    public Result receivePickUpFromHolder(@RequestBody List<Integer> holderIds) {
        // 打印收到的 holderId 列表
        log.info("Received holderIds from Python: " + holderIds);
        merchantHolderClient.updateHoldersFromWhichBeverageWasJustTaken(holderIds);
        return Result.success();
    }
}